﻿using UColor.Core.Helpers;
using DevExpress.Xpf.Core.Native;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;

namespace UCOLOR.App.Extend
{
    [System.ComponentModel.DefaultProperty("Content"), ContentProperty("Content")]
    public class DemoModuleControl : UniversalControl
    {
        public static readonly DependencyProperty OptionsTopTextProperty;
        public static readonly DependencyProperty OptionsContentProperty;
        public static readonly DependencyProperty DemoContentProperty;
        public static readonly DependencyProperty ContentProperty;
        public static readonly DependencyProperty DescriptionProperty;
        public static readonly DependencyProperty LinksProperty;
        public static readonly DependencyProperty IsAboutShownProperty;
        public static readonly DependencyProperty IsAboutMovingProperty;
        public static readonly System.Windows.RoutedEvent BeforeModuleDisappearEvent;

        public event System.Windows.RoutedEventHandler BeforeModuleDisappear
        {
            add
            {
                base.AddHandler(DemoModuleControl.BeforeModuleDisappearEvent, value);
            }
            remove
            {
                base.RemoveHandler(DemoModuleControl.BeforeModuleDisappearEvent, value);
            }
        }
        public string OptionsTopText
        {
            get
            {
                return (string)base.GetValue(DemoModuleControl.OptionsTopTextProperty);
            }
            set
            {
                base.SetValue(DemoModuleControl.OptionsTopTextProperty, value);
            }
        }
        public System.Windows.FrameworkElement Content
        {
            get
            {
                return (System.Windows.FrameworkElement)base.GetValue(DemoModuleControl.ContentProperty);
            }
            set
            {
                base.SetValue(DemoModuleControl.ContentProperty, value);
            }
        }
        public DependencyObject DemoContent
        {
            get
            {
                return (DependencyObject)base.GetValue(DemoModuleControl.DemoContentProperty);
            }
            set
            {
                base.SetValue(DemoModuleControl.DemoContentProperty, value);
            }
        }
        public System.Windows.FrameworkElement OptionsContent
        {
            get
            {
                return (System.Windows.FrameworkElement)base.GetValue(DemoModuleControl.OptionsContentProperty);
            }
            set
            {
                base.SetValue(DemoModuleControl.OptionsContentProperty, value);
            }
        }
        public string Description
        {
            get
            {
                return (string)base.GetValue(DemoModuleControl.DescriptionProperty);
            }
            set
            {
                base.SetValue(DemoModuleControl.DescriptionProperty, value);
            }
        }
        //public DemoModule DemoModule
        //{
        //    get
        //    {
        //        return (DemoModule)base.Parent;
        //    }
        //}
        public System.Windows.FrameworkElement ContentContainer
        {
            get;
            private set;
        }
        public System.Windows.Controls.ScrollViewer OptionsContentContainer
        {
            get;
            private set;
        }
        public System.Windows.FrameworkElement DemoContentElement
        {
            get;
            private set;
        }
        //public CodeViewControl XamlTextViewer
        //{
        //    get;
        //    private set;
        //}
        //public CodeViewControl CodeTextViewer
        //{
        //    get;
        //    private set;
        //}
        //public CornerPopup CornerPopup
        //{
        //    get;
        //    private set;
        //}
        //public DescriptionPresenter DescriptionPresenter
        //{
        //    get;
        //    private set;
        //}
        public System.Collections.Generic.List<object> Links
        {
            get
            {
                return (System.Collections.Generic.List<object>)base.GetValue(DemoModuleControl.LinksProperty);
            }
            set
            {
                base.SetValue(DemoModuleControl.LinksProperty, value);
            }
        }
        public bool IsAboutShown
        {
            get
            {
                return (bool)base.GetValue(DemoModuleControl.IsAboutShownProperty);
            }
            set
            {
                base.SetValue(DemoModuleControl.IsAboutShownProperty, value);
            }
        }
        public bool IsAboutMoving
        {
            get
            {
                return (bool)base.GetValue(DemoModuleControl.IsAboutMovingProperty);
            }
            set
            {
                base.SetValue(DemoModuleControl.IsAboutMovingProperty, value);
            }
        }
        //internal DemoModuleControlLoadState LoadState
        //{
        //    get
        //    {
        //        return this.loadState;
        //    }
        //    set
        //    {
        //        if (this.loadState == value || !System.Enum.IsDefined(typeof(DemoModuleControlLoadState), value))
        //        {
        //            return;
        //        }
        //        this.loadState = value;
        //        VisualStateManager.GoToState(this, "Load" + value.ToString(), true);
        //    }
        //}
        //internal DemoModuleView ViewState
        //{
        //    get
        //    {
        //        return this.viewState;
        //    }
        //    set
        //    {
        //        if (this.viewState == value || !System.Enum.IsDefined(typeof(DemoModuleView), value))
        //        {
        //            return;
        //        }
        //        this.viewState = value;
        //        this.TryEnableCodeSearch();
        //        this.XamlTextViewer.Visibility = ((value == DemoModuleView.Xaml) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed);
        //        this.CodeTextViewer.Visibility = ((value == DemoModuleView.Code) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed);
        //        this.DemoContentElement.Visibility = ((value == DemoModuleView.Demo) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed);
        //        if (this.viewState == DemoModuleView.Demo && !this.IsAboutShown)
        //        {
        //            this.OnShowDemoContent();
        //            return;
        //        }
        //        this.OnHideDemoContent();
        //    }
        //}
        public static DependencyObject FindDemoContent(System.Type tp, DependencyObject root)
        {
            VisualTreeEnumerator visualTreeEnumerator = new VisualTreeEnumerator(root);
            while (visualTreeEnumerator.MoveNext())
            {
                if (visualTreeEnumerator.Current != null && visualTreeEnumerator.Current.GetType() == tp)
                {
                    return visualTreeEnumerator.Current;
                }
            }
            return null;
        }
        public static DemoModuleControl FindParentDemoModuleControl(DependencyObject d)
        {
            for (DependencyObject dependencyObject = d; dependencyObject != null; dependencyObject = System.Windows.Media.VisualTreeHelper.GetParent(dependencyObject))
            {
                if (dependencyObject is DemoModuleControl)
                {
                    return (DemoModuleControl)dependencyObject;
                }
            }
            return null;
        }
        static DemoModuleControl()
        {
            System.Type typeFromHandle = typeof(DemoModuleControl);
            DemoModuleControl.OptionsTopTextProperty = DependencyProperty.Register("OptionsTopText", typeof(string), typeFromHandle, new PropertyMetadata(null));
            DemoModuleControl.ContentProperty = UniversalControl.RegisterContentProperty("Content", typeof(System.Windows.FrameworkElement), typeFromHandle, null, null);
            DemoModuleControl.DemoContentProperty = DependencyProperty.Register("DemoContent", typeof(DependencyObject), typeFromHandle, new PropertyMetadata(null));
            DemoModuleControl.OptionsContentProperty = UniversalControl.RegisterContentProperty("OptionsContent", typeof(System.Windows.FrameworkElement), typeFromHandle, null, null);
            DemoModuleControl.DescriptionProperty = DependencyProperty.Register("Description", typeof(string), typeFromHandle, new PropertyMetadata(null));
            DemoModuleControl.LinksProperty = DependencyProperty.Register("Links", typeof(System.Collections.Generic.List<object>), typeFromHandle, new PropertyMetadata(null));
            DemoModuleControl.IsAboutShownProperty = DependencyProperty.Register("IsAboutShown", typeof(bool), typeFromHandle, new PropertyMetadata(false));
            DemoModuleControl.IsAboutMovingProperty = DependencyProperty.Register("IsAboutMoving", typeof(bool), typeFromHandle, new PropertyMetadata(false));
            DemoModuleControl.BeforeModuleDisappearEvent = System.Windows.EventManager.RegisterRoutedEvent("BeforeModuleDisappear", System.Windows.RoutingStrategy.Direct, typeof(System.Windows.RoutedEventHandler), typeFromHandle);
        }
        public DemoModuleControl()
        {
            base.DefaultStyleKey = typeof(DemoModuleControl);
        }
        public void UpdateOptionsContentDataContext(object dataContext)
        {
            if (dataContext == null)
            {
                dataContext = base.DataContext;
            }
            if (this.OptionsContent != null)
            {
                this.OptionsContent.DataContext = dataContext;
            }
        }
        public void ShowDemo()
        {
  //          this.ViewState = DemoModuleView.Demo;
        }
        public void ShowXaml()
        {
            BackgroundHelper.DoInBackground(null, new System.Action(this.LoadXaml));
        }
        public void ShowCode()
        {
            BackgroundHelper.DoInBackground(null, new System.Action(this.LoadCode));
        }
        public void TryEnableCodeSearch()
        {
            //switch (this.ViewState)
            //{
            //    case DemoModuleView.Code:
            //        this.CodeTextViewer.FocusCodePresenter();
            //        this.XamlTextViewer.CancelSearch();
            //        return;
            //    case DemoModuleView.Xaml:
            //        this.XamlTextViewer.FocusCodePresenter();
            //        this.CodeTextViewer.CancelSearch();
            //        return;
            //    default:
            //        this.XamlTextViewer.CancelSearch();
            //        this.CodeTextViewer.CancelSearch();
            //        return;
            //}
        }
        protected override void OnLoaded(object sender, System.Windows.RoutedEventArgs e)
        {
            base.OnLoaded(sender, e);
        }
        protected internal virtual void RaiseBeforeModuleDisappear()
        {
            base.RaiseEvent(new System.Windows.RoutedEventArgs
            {
                RoutedEvent = DemoModuleControl.BeforeModuleDisappearEvent
            });
        }
        private void LoadXaml()
        {
            //string codeText = DemoHelper.GetXamlText(DemoBasePartsBase.Parts.DemoStaticInfo.DemoAssembly, this.DemoModule.GetType().Name).Replace("\t", "    ");
            //this.XamlTextViewer.CodeText = codeText;
            //this.ViewState = DemoModuleView.Xaml;
            //this.LoadState = DemoModuleControlLoadState.Complete;
        }
        private void LoadCode()
        {
            //string codeText = DemoHelper.GetCodeText(DemoBasePartsBase.Parts.DemoStaticInfo.DemoAssembly, this.DemoModule.GetType().Name).Replace("\t", "    ");
            //this.CodeTextViewer.CodeLanguage = DemoBasePartsBase.Parts.DemoStaticInfo.CodeLanguage;
            //this.CodeTextViewer.CodeText = codeText;
            //this.ViewState = DemoModuleView.Code;
            //this.LoadState = DemoModuleControlLoadState.Complete;
        }
        private void OnHideDemoContent()
        {
            //if (this.DemoModule == null)
            //{
            //    return;
            //}
            //this.DemoModule.IsPopupContentInvisible = true;
        }
        private void OnShowDemoContent()
        {
            //if (this.DemoModule == null)
            //{
            //    return;
            //}
            //this.DemoModule.IsPopupContentInvisible = false;
        }
        protected override void OnApplyTemplateOverride()
        {
            base.OnApplyTemplateOverride();
            //this.UpdateOptionsContentDataContext(this.DemoModule.GetModuleDataContext());
            //this.ContentContainer = (System.Windows.FrameworkElement)base.GetTemplateChild("ContentContainer");
            //this.DemoContentElement = (System.Windows.FrameworkElement)base.GetTemplateChild("DemoContentElement");
            //this.XamlTextViewer = (CodeViewControl)base.GetTemplateChild("XamlTextViewer");
            //this.CodeTextViewer = (CodeViewControl)base.GetTemplateChild("CodeTextViewer");
            //this.CornerPopup = (CornerPopup)base.GetTemplateChild("CornerPopup");
            //this.OptionsContentContainer = (System.Windows.Controls.ScrollViewer)base.GetTemplateChild("OptionsContentContainer");
            //this.DescriptionPresenter = (DescriptionPresenter)base.GetTemplateChild("DescriptionPresenter");
            //CenterLayoutControl.SetDeattachablePanel(this.DemoModule, (this.OptionsContent == null) ? null : this.OptionsContentContainer);
            //CenterLayoutControl.SetGeneralPart(this.DemoModule, this.ContentContainer);
        }
    }
}
